<?php
/*
	[gunianwang system]
	NAME:in_mail_control 站内信管理系统
	AUTHOR:sql
	DATE:2012-07-10
*/

if (!defined('IN_ELS'))
{
    die('Hacking attempt');
}
class control extends in_mailbase {
	var $pagecount 		= 0;
	var $currendnum 	= 1;
	var $pageSize 		= 50;	//每页多少条
	var $pageNumSize 	= 10;	//分页数
	
	function control() {
		$this->in_mailbase();
		if(!empty($_REQUEST['currendnum'])){
			$this->currendnum=$_REQUEST['currendnum'];
		}
		$act=empty($_REQUEST['a']) ? 'empty' : $_REQUEST['a'];
		
		if($act=="empty")				$this->in_mailList();//站内信会员发送首页
		if($act=='send_mail')			$this->send_mail();//站内信发送
		if($act=='list')				$this->list_mail();//站内信列表
		if($act=='maildel')				$this->maildel();//站内信删除
		if($act=='maildelall')				$this->maildelAll();//站内信批量删除
		if($act=='mailinfo')				$this->mailinfo();//站内信详细
		if($act=='mailsave')				$this->mailsave();//保存站内信
		
		
	}  
	function in_mailList()                //主页显示
	{
		// news List
		
		$where = "where 1=1 and is_pingbi=1";
		$url = "";
		if(!empty($_REQUEST['search']) && $_REQUEST['search']=='search'){//搜索
				
				$search_1 = empty($_REQUEST['user_leve']) ?'':intval($_REQUEST['user_leve']);// 会员等级
				if($search_1!=''){
					$where .=" and user_leve=$search_1 ";
				}
			}
		$dataCount=$this->getmembersCount($where);
		$startNum = $this->pageSize*($this->currendnum-1);
		$limitText = " limit ".$startNum." , ".$this->pageSize;
		$pagecountdata=array();
		require_once ELS_ROOT.'lib/paging.class.php';
		$pg =  new paging();
		$pg -> setLinkText("<a href='admin.php?p=in_mail$url&currendnum=!!PageNum!!'>!!LinkText!!</a>");
		$pg -> setCurrendnum(  $this-> currendnum);
		$pg -> setPageSize(    $this-> pageSize);
		$pg -> setPageNumSize( $this-> pageNumSize);
		$pg -> setDataCount(   $dataCount);
		$pagecountdata = $pg ->makePageText();
		
		$list=$this->getmembersList($where,$limitText);
		$this->view->assign('list',$list);
		$this->view->assign('pagecount' ,$pagecountdata);
		$this->view->assign('currendnum',$currendnum);
		$this->view->assign('user_leve',$search_1);
		$this->view->display('in_mailList');

	}
	function send_mail()					//发送站内信
	{
		$user_ids = array();
		$user_ids = $_REQUEST['ids'];
		$title = $_REQUEST['titles'];
		$content =  $_REQUEST['contents'];
		$time = $this->getCurrTimeLine()+8*3600;//时间差 加8小时
		$data_arr = array(
			'title' =>$title,
			'content' =>$content,
			'create_time'=>$time,
			'admin' => $_SESSION["admin"]['uid'],
			'is_read'=>1,
			'u_id' =>$_SESSION["user_id"],
		);
		if($user_ids==null)// 判断是否选择用户
		{
			echo " <script>alert('请选择一个用户');</script> ";
			$url_t = "?p=in_mail";
			
		}
		else{
			foreach($user_ids as $k=>$v) 
			{
				$data_arr['u_id']=$v;
				$this->insertMail($data_arr);
			}
			echo "<script> alert('发送完毕');</script>"; 
			$url_t = "?p=in_mail&a=list";
		}
		$this->pageRedirect($url_t);
	}
	// 站内信列表
	function list_mail()
	{
		$where = " where 1=1 ";
		
		$dataCount=$this->getmailCount($where);
		$startNum = $this->pageSize*($this->currendnum-1);
		$limitText = " limit ".$startNum." , ".$this->pageSize;
		$pagecountdata=array();
		require_once ELS_ROOT.'lib/paging.class.php';
		$pg =  new paging();
		$pg -> setLinkText("<a href='admin.php?p=in_mail$url&currendnum=!!PageNum!!'>!!LinkText!!</a>");
		$pg -> setCurrendnum(  $this-> currendnum);
		$pg -> setPageSize(    $this-> pageSize);
		$pg -> setPageNumSize( $this-> pageNumSize);
		$pg -> setDataCount(   $dataCount);
		$pagecountdata = $pg ->makePageText();
		
		$list=$this->getmailList($where,$limitText);
		$this->view->assign('list',$list);
		$this->view->assign('pagecount' ,$pagecountdata);
		$this->view->assign('currendnum',$currendnum);
		$this->view->display("admin_mail_list");
	}
	// 删除单个mail站内信
	function maildel(){
		$mail_id = empty($_REQUEST['mail_id'])?"":$_REQUEST['mail_id'];
		if($mail_id!='')
		{
			$this->db->query("DELETE FROM gnw_in_mail WHERE id = '$mail_id' limit 1");
			echo "<script>alert('删除成功！');</script>";
			$this->pageRedirect("?p=in_mail&a=list");
		}
		
	}
	function maildelAll()
	{
		$mail_id= empty($_POST['ids'])?array():$_POST['ids'];
		//print_r($mail_id);exit;
		//删除
		if($mail_id){
			for($i=0;$i<count($mail_id);$i++){
				
				$this->db->query("DELETE FROM gnw_in_mail WHERE id = '$mail_id[$i]'");
			}
			echo "<script>alert('批量删除成功！');</script>";
		}
		
		$this->pageRedirect("?p=in_mail&a=list");
	}

	function mailinfo()
	{
		$mail_id = $_REQUEST['mail_id'];
		$sql_info  = "select id,title,content,FROM_UNIXTIME((create_time-8*3600),'%Y-%m-%d %H:%i:%s') create_time,admin,is_read,u_id from gnw_in_mail where id=".$mail_id." limit 1";
		$data = $this->db->fetch_first($sql_info);
		
			$sql_user_name ="select user_name from gnw_user where id=".$data['u_id']." limit 1";
			$user_name=$this->db->result_first($sql_user_name);
			$data['user_name'] = $user_name;
			$sql_admin_name = "select username from admins where uid=".$data['admin'];
			$admin_name = $this->db->result_first($sql_admin_name);
			$data['admin_name'] = $admin_name;
	
		$this->view->assign('infos',$data);
		$this->view->display("admin_mail_info");
	}
	function mailsave()
	{
		
		$mail_id = $_REQUEST['mail_id'];
		$where = " id = ".$mail_id;
		$data_arr['title'] = $_REQUEST['title'];
		$data_arr['content'] = $_REQUEST['content'];

		$this->updateinmail($data_arr,$where);
		echo "<script>alert('修改成功！');</script>";
		$this->pageRedirect("?p=in_mail&a=mailinfo&mail_id=".$mail_id);
		
	}



}
?>